package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import com.woniuxy.oldmanapi.entity.Contract;
import com.woniuxy.oldmanapi.entity.Outapplication;
import com.woniuxy.oldmanapi.service.ContractService;
import com.woniuxy.oldmanapi.service.OutapplicationService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.yvo.ContractVo;
import com.woniuxy.oldmanapi.yvo.OutApplicationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
public class ContractController {
    @Autowired
    ContractService contractService;

    /**
     * 此方法为初始化外出申请表页面数据
     *
     * @param name 搜索框条件
     * @param now  分页 页数
     * @param size 分页 条数
     * @return
     */
    @GetMapping("/contract/list")
    public ResponseResult<PageInfo<ContractVo>> initSelectAllByCons(String name, int now, int size) {
        ResponseResult<PageInfo<ContractVo>> result = new ResponseResult<>();
        try {
            PageInfo<ContractVo> pageInfo = contractService.initSelectAllByCons(name, now, size);
            if (pageInfo.getList() == null) {
                result.setCode(204);
                result.setMsg("无满足条件对象");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    /**
     * 此方法用于删除一个合同模板
     *
     * @param id
     * @return
     */
    @DeleteMapping("/contract/deleteContract/{id}")
    public ResponseResult<Void> deleteContract(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            contractService.deleteContract(id);
            result.setMsg("删除成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    /**
     * 此方位用于 修改一个合同的状态 修改为启用或停用
     *
     * @param id     此合同对象ID
     * @param status 0 停用, 1 启用
     * @return
     */
    @PutMapping("/contract/modifyContractStatus/{id}/{status}")
    public ResponseResult<Void> modifyContractStatus(@PathVariable("id") int id, @PathVariable("status") int status) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            contractService.modifyContractStatus(id, status);
            if (status == 0) {
                result.setMsg("停用成功");
                result.setCode(200);
            } else {
                result.setMsg("启用成功");
                result.setCode(200);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    /**
     * 此方位用于 修改一个合同的信息
     *
     * @param contract 修改的合同对象
     * @param
     * @return
     */
    @PutMapping("/contract/modifyContract")
    public ResponseResult<Void> modifyContractStatus(@RequestBody Contract contract) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            contractService.modifyContract(contract);
            result.setMsg("修改成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
    /**
     * 此方法用于 打开编辑修改时 查询一个对象 返显使用
     *
     * @param id 查询对象的Id
     * @return
     */

    @GetMapping("/contract/selectContract/{id}")
    public ResponseResult<ContractVo> selectContractById(@PathVariable("id") int id) {
        ResponseResult<ContractVo> result = new ResponseResult<>();
        try {
            ContractVo contractVo = contractService.selectContractById(id);
            if (contractVo == null) {
                result.setCode(202);
                result.setMsg("此对象信息有误,请稍后再试");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(contractVo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }


    /**
     * 此方法为添加合同
     *
     * @param contract 合同对象 非 vo
     * @return
     */
    @PostMapping("/contract/addContract")
    public ResponseResult<Void> addContract(@RequestBody Contract contract) {
        /*测试*/
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            contractService.addContract(contract);
            result.setMsg("创建成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }


}
